﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using Model;

namespace ACM
{
    public class BasePage : System.Web.UI.Page
    {
        public int BusinessID
        {
            get
            {
                if (Request.QueryString["id"] == null)
                    return 0;
                else
                {
                    try
                    {
                        return Convert.ToInt32(Request.QueryString["id"]);
                    }
                    catch (Exception)
                    {
                        return 0;
                    }

                }
            }
        }

        public acm_user CurrentUser
        {
            get
            {
                if (Session["CurrentUser"] == null)
                {
                    Response.Redirect("Default.aspx");

                }

                return (acm_user)Session["CurrentUser"];
            }
            set
            {
                Session["CurrentUser"] = value;
            }
        }

        public void NeedAllowRole(string role, string targetPage)
        {
            if (CurrentUser.user_role != role)
                Response.Redirect(targetPage);
        }

        public void NotAllowRole(string role, string targetPage)
        {
            if (CurrentUser.user_role == role)
                Response.Redirect(targetPage);
        }

        public void ShowControlWith(Control ctrl, string role)
        {
            if (CurrentUser.user_role != role)
                ctrl.Visible = false;
        }

        public void HideControlWith(Control ctrl, string role)
        {
            if (CurrentUser.user_role == role)
                ctrl.Visible = false;
        }

        public void HideColumnWith(DataControlField ctrl, string role)
        {
            if (CurrentUser.user_role == role)
                ctrl.Visible = false;
        }

        protected void Page_Init(object sender, EventArgs e)
        {
           //Bypass("dealer2", "1111");
        }

        protected void Bypass(string uname, string pwd)
        {
            GetUserCommand cmd = new GetUserCommand(uname, pwd);
            cmd.Execute();
            Session["CurrentUser"] = cmd.Result;
        }

    }
}